﻿namespace Example.Creational
{
    using Example.Creational.ObjectPool;

    using Xunit;

    /// <summary>
    /// Avoid expensive acquisition and release of resources by recycling objects that are no longer in use.
    /// Can be considered a generalisation of connection pool and thread pool patterns.
    /// </summary>
    public sealed class ObjectPoolFacts
    {
        [Fact]
        public void car_pooling()
        {
            var one = new Car("ABC 111");
            var two = new Car("ABC 222");
            CarPool.Add(one, two);

            var first = CarPool.Get();
            Assert.Same(one, first);

            var second = CarPool.Get();
            Assert.Same(two, second);

            Assert.Null(CarPool.Get());

            CarPool.Return(first);
            CarPool.Return(second);

            second = CarPool.Get();
            Assert.Same(one, second);
        }
    }
}